package com.kgc.basicprovider.service.impl;

import com.kgc.basicprovider.dao.PublicDao;
import com.kgc.basicprovider.service.PublicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class PublicServiceImpl implements PublicService {

    private Logger logger = LoggerFactory.getLogger(PublicServiceImpl.class);

    @Resource
    private PublicDao publicDao;

    @Override
    public String getCode(String table) {
        Date data = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String date = format.format(data);
        Map<String,String> map = new HashMap<String,String>();
        map.put("table",table);
        map.put("code",date);
        int i = publicDao.getCode(map);
        StringBuffer sb = new StringBuffer(String.valueOf(i));
        for(i = sb.length() ; i< 4 ; i++){
            sb.insert(0,"0");
        }
        String code =date +"-"+ sb.toString();
        logger.info("getCode map"+ map + "num"+i);
        return code;
    }
}
